package com.example.springboot_jap_model.controller;

import com.example.springboot_jap_model.constant.Constant;
import com.example.springboot_jap_model.domain.Authority;
import com.example.springboot_jap_model.domain.RespEntity;
import com.example.springboot_jap_model.domain.User;
import com.example.springboot_jap_model.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 处理 用户相关的操作行为
 * 1.用户增加
 * 2.用户删除 (逻辑删除，-1）
 * 3.用户权限操作
 * 4.用户查询
 * ...
 * 等等操作
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @RequestMapping("addUser")
    @ResponseBody
    public RespEntity addUser(User user) {
        return userService.addUser(user);
    }

    @PostMapping("doLoin")
    public String doLogin(User user) {
        RespEntity respEntity = userService.doLogin(user);

        if (Constant.OPERATING_SUCCESS == respEntity.getCode()) {
            return "redirect:/main/";
        }
        return "redirect:/login";
    }

    @RequestMapping("logOut")
    public String doLogOut() {
        userService.doLogOut();
        return "redirect:/login";
    }

    /**
     * 添加权限
     *
     * @param authority
     * @return
     */
    @PostMapping("addAuth")
    @ResponseBody
    public RespEntity addAuth(Authority authority) {
        return userService.addAuth(authority);
    }

    /**
     * 权限分配
     * @param strs
     * @return
     */
    @PostMapping("distributionAuth")
    @ResponseBody
    public RespEntity distributionAuth(@RequestParam("auth_str")String strs){

        return userService.updateAuto(strs);
    }

    /**
     *查询用户
     */
    @PostMapping("/findAllUser")
    @ResponseBody
    public RespEntity findAllUser(@RequestParam("page")int page){
        return userService.findByTypeAndStatus(Constant.PT_USER_TYPE,Constant.SUCCESS_STATIC,page);
    }


}
